import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shoes_shop/api/api.dart';
import 'package:shoes_shop/core/utils.dart';
import 'package:shoes_shop/models/public_provider_model.dart';

class InputTextPage extends StatelessWidget {
  final String nickname;
  InputTextPage({Key key, this.nickname}) : super(key: key);
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    _controller.text = nickname;
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(),
        actions: [
          IconButton(
              icon: Icon(Icons.check),
              onPressed: () async {
                print(_controller.text);
                Provider.of<PublicProviderModel>(context, listen: false)
                    .changeUserInfoValue('nickname', _controller.text);
                Map userInfo =
                    Provider.of<PublicProviderModel>(context, listen: false)
                        .userInfo;
                Map res = await updateUserInfo(userInfo);
                showToast(res['message']);
                if (res['code'] == 200) {
                  Navigator.pop(context);
                }
              }),
        ],
      ),
      body: Container(
        padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
        child: TextField(
          controller: _controller,
          maxLength: 20,
          buildCounter: (
            BuildContext context, {
            int currentLength,
            int maxLength,
            bool isFocused,
          }) {
            return Text(
              '$currentLength/$maxLength',
            );
          },
          decoration: InputDecoration(
            hintText: '请输入你的昵称',
            hintStyle: TextStyle(color: Colors.grey[400]),
            hintMaxLines: 1,
            enabledBorder: UnderlineInputBorder(
              borderSide: BorderSide(color: Colors.grey),
            ),
            focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(color: Colors.grey),
            ),
          ),
        ),
      ),
    );
  }
}
